package com.stars.empoly.utils

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.view.LayoutInflater
import android.view.View
import android.widget.*
import com.qmuiteam.qmui.kotlin.onClick
import com.qmuiteam.qmui.layout.QMUIFrameLayout
import com.qmuiteam.qmui.skin.QMUISkinValueBuilder
import com.qmuiteam.qmui.util.QMUIDisplayHelper
import com.qmuiteam.qmui.widget.popup.QMUIPopup
import com.qmuiteam.qmui.widget.popup.QMUIPopups
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton
import com.stars.empoly.R

/**
 * 导航-调用手机自带的 高德/百度
 *Created by shihuiyun
 *on 2020/9/21
 */
object  PopuGuideUtils {

    fun show(context: Context,  position:String,address: String, lat: String, lng: String, view: View){

        var mNormalPopup = QMUIPopups.fullScreenPopup(context)
        val builder: QMUISkinValueBuilder = QMUISkinValueBuilder.acquire()
        val frameLayout = QMUIFrameLayout(context)
        //自定义布局
        val layoutInflater = context?.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        var layout = layoutInflater.inflate(R.layout.item_guide, null)

        val tv1:TextView=layout.findViewById(R.id.tv1)
        val tv2:TextView=layout.findViewById(R.id.tv2)
        val line1:View=layout.findViewById(R.id.line1)
        val line2:View=layout.findViewById(R.id.line2)

        val tvPosition:TextView=layout.findViewById(R.id.tv_position)
        val tvAddress:TextView=layout.findViewById(R.id.tv_address)
        val ll:LinearLayout=layout.findViewById(R.id.ll)
        val butCancel: QMUIRoundButton =layout.findViewById(R.id.but_cancel)


        tvPosition.text=position
        tvAddress.text=address

        if (CheckUtil.isAppInstalled(context, "com.baidu.BaiduMap")) {
        } else {
            tv1.visibility = View.GONE
            line2.visibility= View.GONE
        }


        if (CheckUtil.isAppInstalled(context, "com.autonavi.minimap")) {
        } else {
            tv2.visibility = View.GONE
            line1.visibility= View.GONE
        }

        tv1.onClick {
            //百度导航

//        String uri = "baidumap://map/direction"
//                + "?origin=我的位置"
//                + "&destination=" + name + "&latlng:" + latitude + "," + longitude
//                + "&coord_type=bd09ll"
//                + "&mode=driving"
//                + "&src=andr.companyName.appName";//src为统计来源必填，companyName、appName是公司名和应用名
//        Intent intent = new Intent("android.intent.action.VIEW", Uri.parse(uri));
//        intent.addCategory("android.intent.category.DEFAULT");
//        context.startActivity(intent);
            val i1 = Intent()

            // 驾车路线规划
            i1.data = Uri.parse("baidumap://map/direction?origin=我的位置&destination=" +
                    position + "&latlng:" +
                    lat + "," + lng +
                    "&coord_type=bd09ll&mode=driving&src=andr.baidu.openAPIdemo")

            context.startActivity(i1)


            mNormalPopup.dismiss()
        }

        tv2.onClick {
            //高德导航

            //默认驾车

            //默认驾车
            var uriString: String? = null
            val builder = StringBuilder("amapuri://route/plan?sourceApplication=maxuslife")
            //如果不传起点（注释下面这段），默认就是现在我的位置（手机当前定位）
            //如果不传起点（注释下面这段），默认就是现在我的位置（手机当前定位）
            builder.append("&sname=我的位置")
                    .append("&dlat=").append(lat)
                    .append("&dlon=").append(lng)
                    .append("&dname=").append(position)
                    .append("&dev=0")
                    .append("&t=0")
            uriString = builder.toString()
            val intent = Intent(Intent.ACTION_VIEW)
            intent.setPackage("com.autonavi.minimap")
            intent.data = Uri.parse(uriString)
            context.startActivity(intent)


            mNormalPopup.dismiss()
        }



        butCancel.onClick {
            mNormalPopup.dismiss()
        }
        ll.onClick {
            mNormalPopup.dismiss()
        }

        builder.release();

        val size = QMUIDisplayHelper.getScreenWidth(context)
        val height = QMUIDisplayHelper.getScreenHeight(context)


        val lp: FrameLayout.LayoutParams = FrameLayout.LayoutParams(size,
                height)
        frameLayout.addView(layout, lp)


        mNormalPopup.addView(frameLayout)
        mNormalPopup.onDismiss() {
        }

        mNormalPopup.dismissIfOutsideTouch(false)
        mNormalPopup.animStyle(QMUIPopup.ANIM_GROW_FROM_CENTER)
        mNormalPopup.show(view)


    }




}
